request = {}

request.heartbeat = function() {
  client.request('heartbeat', '', function(res) {})
}


request.createPlayer = function(name, username, password, str, con, agile, wisdom, cb) {
  client.request('createPlayer', {
    name: name,
    username: username,
    password: password,
    str: str,
    con: con,
    agile: agile,
    wisdom: wisdom
  }, function(res) {
    if (res.error === 0) {
      if (cb)
        cb()
    } else {
      $('.warning').html('*角色名已存在')
    }
  })
}

request.playerLogin = function(username, password, cb) {
  client.request('playerLogin', {
    username: username,
    password: password,
  }, function(res) {
    if (res.error === 0) {
      //心跳
      heartbeatTimer = setInterval(function() {
        request.heartbeat() //每分钟
      }, 60000)

      if (res.player) {
        player = res.player
      }
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      player.name = res.name
      latticeIndex = player.basicInfo.lattice
        //  client.request('getPropName', '', function(res) {
        //获取物品id对应名字信息
        //  propName = res.propName
        //  })
      $('#main').html(template('t_playerMain'))
      var mapIndex = player.basicInfo.mapIndex
        //  request.getTemplateConfig(function() { //获取服务端配置
        //  })
      load()

      function load() {
        if (mapIndex === 1) {
          loadSecretMain()
        } else if (mapIndex === 2) {
          loadSecretEntrance()
        } else if (mapIndex === 3) {
          loadSecretMap()
        } else if (mapIndex === 4) {
          loadSecretGuard()
        } else if (mapIndex === 5) {
          loadDevilCave()
        }
      }

      if (cb) {
        cb()
      }

    } else if (res.error === 1) {
      //request.getTemplateConfig(function() {  })
      loadCreatePlayer()

    }
  })
}

//获取玩家信息
request.getPlayerInfo = function(cb) {
  //获取玩家信息
  client.request('getPlayerInfo', '', function(res) {
    if (res.error === 0) {
      player = res.player
    }
    if (cb) {
      cb()
    }
  })
}

//进入秘境
request.enterSecretScene = function(secretId) {
  client.request('enterSecretScene', {
    secretId: secretId
  }, function(res) {
    if (res.error === 0) {
      $('#result').html(template($('#sercet').text()))
      loadSercet()
    }
  })
}

//冒险
request.adventure = function() {
  client.request('adventure', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      loadSecretMap(res)
    }
  })
}

//进入深处岗哨
request.enterDeepPost = function(cb) {
  client.request('enterDeepPost', '', function(res) {
    if (res.error === 0) {
      player.basicInfo.lattice = res.lattice
      loadSecretMap(res)
      if (cb)
        cb(res)
    }
  })
}

//跑尸
request.corpse = function(cb) {
  client.request('corpse', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      if (cb)
        cb(res)
    }
  })
}

//挑战副本
request.attackFam = function(cb) {
  client.request('attackFam', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      if (res && res.state) {
        loadSecretMain()
        return
      }
      if (cb)
        cb(res)
    }
  })
}

//采集
request.collection = function(cb) {
  client.request('collection', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      if (res && res.state) {
        loadSecretMain()
        return
      }
      if (cb)
        cb(res)
    }
  })
}

//挖矿
request.mining = function(cb) {
  client.request('mining', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      if (res && res.state) {
        loadSecretMain()
        return
      }
      if (cb)
        cb(res)
    }
  })
}

//钓鱼
request.fishing = function(cb) {
  client.request('fishing', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      if (res && res.state) {
        loadSecretMain()
        return
      }
      if (cb)
        cb(res)
    }
  })
}

//神秘商店
request.getSecretMerchant = function(cb) {
  client.request('getSecretMerchant', '', function(res) {
    if (res.error === 0) {
      if (cb)
        cb(res)
    }
  })
}

//购买神秘商店物品
request.buySecretMerchant = function(index, cb) {
  client.request('buySecretMerchant', {
    index: index
  }, function(res) {
    if (res.error === 0) {
      if (cb)
        cb(res)
    } else {
      showMessage('勋章不足,无法兑换')
    }
  })
}


//前往秘境入口
request.entrance = function() {
  client.request('entrance', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      loadSecretEntrance()
    }
  })
}

//开启秘境
request.createSecret = function(secretMapId, cb) {
  client.request('createSecret', {
    secretMapId: secretMapId
  }, function(res) {
    if (res.error === 0) {
      player.basicInfo.secretMap = res.secretMap
      var secretMap = res.secretMap
      if (cb)
        cb()
    }
  })
}

//进入秘境
request.entranceSecret = function() {
  client.request('entranceSecret', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      loadSecretMap()
      showMessage('你进入了树海秘境')
    }
  })
}

//前往深处秘境
request.deepSecret = function(cb) {
  client.request('deepSecret', '', function(res) {
    if (res.error === 0) {
      cb(res)
    }
  })
}

//前往深处秘境
request.getDeepSecret = function(cb) {
  client.request('getDeepSecret', '', function(res) {
    if (res.error === 0) {
      cb(res)
    }
  })
}

//泡温泉
request.hotSpring = function(cb) {
  client.request('hotSpring', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      if (res && res.state) {
        loadSecretMain()
        return
      }
      if (cb) {
        cb()
      }
    }
  })
}

//护送物质
request.transport = function(cb) {
  client.request('transport', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      cb(res.transportInfos, res.getProp)
    }
  })
}

//返回营地
request.returnCamp = function(cb) {
  client.request('returnCamp', '', function(res) {
    if (res.basicInfo) {
      player.basicInfo = res.basicInfo
    }
    loadSecretMain()
    if (cb) {
      cb(res)
    }
  })
}

//返回营地
request.altarReturnCamp = function(cb) {
  client.request('altarReturnCamp', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      loadSecretMain()
      showMessage('你回到了营地,恢复了所有的状态')
      if (cb) {
        cb()
      }
    }
  })
}

//离开魔窟
request.exitDevilCave = function(cb) {
  client.request('exitDevilCave', '', function(res) {
    if (res.error === 0) {
      if (cb) {
        cb(res)
      }
    }
  })
}

//穿戴装备
request.equip = function(pos, cb) {
  client.request('equip', {
    pos: pos
  }, function(res) {
    if (res.error === 0) {
      if (cb) {
        cb()
      }
    }
  })
}

//卸下装备
request.disEquipment = function(part, cb) {
  client.request('disEquipment', {
    part: part
  }, function(res) {
    if (cb) {
      cb(res)
    }
  })
}

//加属性
request.attriPointPlus = function(str, con, agile, wisdom) {
  client.request('attriPointPlus', {
    str: str,
    con: con,
    agile: agile,
    wisdom: wisdom
  }, function(res) {
    if (res.error === 0) {
      loadPlayerInfo()
    }
  })
}


//丢弃物品
request.discard = function(pos, cb) {
  client.request('discard', {
    pos: pos
  }, function(res) {
    if (res.error === 0) {
      cb()
    }
  })
}


//合成物品
request.itemCompose = function(itemId, cb) {
  client.request('itemCompose', {
    itemId: itemId
  }, function(res) {
    cb(res)
  })
}

//强化装备
request.strengthen = function(part, cb) {
  client.request('strengthen', {
    part: part
  }, function(res) {
    cb(res)
  })
}

//报名试炼之所
request.joinTrial = function(cb) {
  client.request('joinTrial', '', function(res) {
    if (res.error === 0) {
      cb(res)
    }
  })
}

//获取试炼之所信息
request.getTrialInfo = function(cb) {
  client.request('getTrialInfo', '', function(res) {
    if (res.error === 0) {
      player['trialInfo'] = res.trialInfo
      if (cb) {
        cb(res)
      }
    }
  })
}

//是否参加试炼
request.getHasJoinTrial = function(cb) {
  client.request('getHasJoinTrial', '', function(res) {
    if (res.error === 0) {
      if (cb) {
        cb(res)
      }
    }
  })
}

//获取邮件
request.getMails = function(page, cb) {
  client.request('getMails', {
    page: page
  }, function(res) {
    if (res.error === 0) {
      if (cb) {
        cb(res)
      }
    }
  })
}

request.deleteMail = function(mailId, cb) {
  client.request('deleteMail', {
    mailId: mailId
  }, function(res) {
    cb(res)
  })
}

//提取邮件
request.extractMail = function(mailId, cb) {
  client.request('extractMail', {
    mailId: mailId
  }, function(res) {
    cb(res)
  })
}

//使用藏宝图
request.useTreasure = function(cb) {
  client.request('useTreasure', '', function(res) {
    if (res.error === 0) {
      if (cb) {
        cb(res)
      }
    }
  })
}


//攻击盗宝地精
request.attackThief = function(cb) {
  client.request('attackThief', '', function(res) {
    if (res.error === 0) {
      if (res && res.state) {
        loadSecretMain()
        return
      }
      if (cb) {
        cb(res)
      }
    }
  })
}

//删除冒险背包物品
request.deleteABag = function(value, cb) {
  client.request('deleteABag', {
    index: value
  }, function(res) {
    if (cb) {
      cb(res)
    }
  })
}

//提取背包物品
request.extractABag = function(cb) {
  client.request('extractABag', '', function(res) {
    if (res.error === 0) {
      if (cb) {
        cb(res)
      }
    }
  })
}


//打孔
request.punch = function(part, cb) {
  client.request('punch', {
    part: part
  }, function(res) {
    if (res.error === 0) {
      if (cb) {
        cb(res)
      }
    }
  })
}

//镶嵌宝石
request.setGem = function(part, pos, cb) {
  client.request('setGem', {
    part: part,
    pos: pos
  }, function(res) {
    if (res.error === 0) {
      if (cb) {
        cb(res)
      }
    }
  })
}

//摘除宝石
request.removeGam = function(part, cb) {
  client.request('removeGam', {
    part: part
  }, function(res) {
    if (res.error === 0) {
      if (cb) {
        cb(res)
      }
    }
  })
}

//攻击野外boss
request.attackFieldBoss = function(cb) {
  client.request('attackFieldBoss', '', function(res) {
    if (cb) {
      cb(res)
    }
  })
}

//祭坛复活
request.altarRevive = function(cb) {
  client.request('altarRevive', '', function(res) {
      if (cb) {
        cb(res)
      }
  })
}

//打技能书
request.setSkillBook = function(pos, cb) {
  client.request('setSkillBook', {
    pos: pos
  }, function(res) {
    if (cb) {
      cb(res)
    }
  })
}


//进入魔窟
request.enterDevilCave = function(cb) {
  client.request('enterDevilCave', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      if (cb) {
        cb(res)
      }
    }
  })
}

//攻击魔窟怪物,包括boss
request.attackDevilCave = function(cb) {
  client.request('attackDevilCave', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      if (cb) {
        cb(res)
      }
    }
  })
}

request.getTemplateConfig = function(cb) {
  client.request('test_getTemplateConfig', '', function(res) {
    if (res.error === 0) {
      templateConfig = res.templateConfig
      if (cb) {
        cb()
      }
    }
  })
}

request.triggerEvent = function(cb) {
  client.request('triggerEvent', '', function(res) {
    if (res.basicInfo) {
      player.basicInfo = res.basicInfo
    }
    cb(res)
    loadSecretMap()

  })
}

//祈祷
request.pray = function(cb) {
  client.request('pray', '', function(res) {
    if (res.error === 0) {
      if (res.basicInfo) {
        player.basicInfo = res.basicInfo
      }
      if (res && res.state) {
        loadSecretMain()
        return
      }
      loadSecretMap()
      if (cb) {
        cb(res)
      }
    }
  })
}

//保存角色
request.savePlayer = function() {
  client.request('savePlayer', '', function() {})
}

//整理仓库
request.arrange = function(cb) {
  client.request('arrange', '', function(res) {
    if (res.error === 0) {
      if (cb)
        cb()
    }
  })
}

//获取背包信息仓库
request.getBagInfo = function(cb) {
  client.request('getBagInfo', '', function(res) {
    if (cb)
      cb(res)
  })
}

//扩展背包
request.addBagSize = function(cb) {
  client.request('addBagSize', '', function(res) {
    if (cb)
      cb(res)
  })
}

//使用物品
request.useProp = function(bagIndex, cb) {
  client.request('useProp', {
    pos: bagIndex
  }, function(res) {
    if (cb)
      cb(res)
  })
}

//竞技场
request.getArenaMyInfo = function(cb) {
  client.request('getArenaMyInfo', '', function(res) {
    cb(res)
  })
}

//竞技场
request.getArenaInfo = function(cb) {
  client.request('getArenaInfo', '', function(res) {
    cb(res)
  })
}

//竞技场
request.arenaChallenge = function(index, cb) {
  client.request('arenaChallenge', {
    index: index
  }, function(res) {
    cb(res)
  })
}

//出售物品到交易所
request.sellToTrade = function(pos, sellGold, num, cb) {
  client.request('sellToTrade', {
    pos: pos,
    sellGold: sellGold,
    num: num
  }, function(res) {
    cb(res)
  })
}

//获取交易所信息
request.getTradeInfo = function(sellType, page, cb) {
  client.request('getTradeInfo', {
    sellType: sellType,
    page: page
  }, function(res) {
    cb(res)
  })
}

//交易所购买东西
request.bugTrade = function(tradeId, cb) {
  client.request('bugTrade', {
    tradeId: tradeId
  }, function(res) {
    cb(res)
  })
}

//获取我的交易所信息
request.getMyTradeInfo = function(cb) {
  client.request('getMyTradeInfo', '', function(res) {
    cb(res)
  })
}

//下架我的物品
request.cancelSell = function(tradeId, cb) {
  client.request('cancelSell', {
    tradeId: tradeId
  }, function(res) {
    cb(res)
  })
}

//获取基本属性
request.getBasicInfo = function(cb) {
  client.request('getBasicInfo', '', function(res) {
    cb(res)
  })
}

//获取战斗属性
request.getFightAttri = function(cb) {
  client.request('getFightAttri', '', function(res) {
    cb(res)
  })
}

//获取冒险信息栏信息
request.getAdvInfo = function(cb) {
  client.request('getAdvInfo', '', function(res) {
    cb(res)
  })
}

//获取装备信息
request.getEquipments = function(cb) {
  client.request('getEquipments', '', function(res) {
    cb(res)
  })
}

//获取冒险背包信息
request.getAdventureBag = function(cb) {
  client.request('getAdventureBag', '', function(res) {
    cb(res)
  })
}

//获取冒险信息栏信息
request.getPlayer = function(cb) {
  client.request('getPlayer', '', function(res) {
    cb(res)
  })
}

request.useCureBar = function(cb) {
  client.request('useCureBar', '', function(res) {
    cb(res)
  })
}

request.takeAlms = function(cb) {
  client.request('takeAlms', '', function(res) {
    cb(res)
  })
}
